<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>授予权限</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20" class="el-row-table">
        <el-col :span="20">
          <div class="grid-content bg-purple">
            <el-button type="primary" @click="toRole">返回</el-button>
          </div>
        </el-col>
      </el-row>

      <!-- 角色填写区域 -->
      <el-form label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input type="text" v-model="roleObject.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="角色备注" prop="note">
          <el-input type="text" v-model="roleObject.note" disabled></el-input>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 学院列表区域 -->
      <el-table border stripe width="100%" :data="warrantList">
        <el-table-column fixed prop="id" label="权限编号"> </el-table-column>
        <el-table-column fixed prop="note" label="权限名"> </el-table-column>
        <el-table-column fixed prop="name" label="权限标识"> </el-table-column>

        <el-table-column fixed label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.flag"
              @change="roleWarrantChanged(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { lockRoles, unlockRoles, getRoles } from "@/api/rolesmanagement.js";
export default {
  data() {
    return {
      //获取课程列表的参数对象
      queryInfo: {
        current: 1,
        size: 5,
        name: "",
      },
      warrantList: [],
      total: 0,
      roleObject: {},
    };
  },
  created() {
    this.getWarrantList();
  },
  methods: {
    async getWarrantList() {
      var id = JSON.parse(localStorage.getItem("roleId"));
      const { data: res } = await getRoles(id);
      this.roleObject = res.data;
      this.warrantList = res.data.auths;
      this.total = this.warrantList.length;
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.size = newSize;
      this.getWarrantList();
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.current = newPage;
      this.getWarrantList();
    },
    //跳转回角色界面
    toRole() {
      this.$router.push("/auth/role");
    },
    //监听switch开关状态的改变
    roleWarrantChanged(rolewarrant) {
      var roleId = this.roleObject.id;
      var authId = rolewarrant.id;
      //如果rolewarrant.flag是false，说明它要执行的是不授权的操作
      if (rolewarrant.flag == false) {
        unlockRoles(roleId, authId);
      } else {
        lockRoles(roleId, authId);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.breadcrumb {
  margin-bottom: 15px;
  font-size: 15px;
}
.box-card {
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15) !important;
}
.el-row-table {
  margin-bottom: 15px !important;
}
.el-pagination {
  margin-top: 15px;
}
</style>